using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCServiceModule : RepositoryItemUI
	{
		#region Initialization

		public UCServiceModule(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			InitializeComponent();
			init();
		}

		private void init()
		{
		}

		#endregion

		#region Public Members

		#region Properties
		public override IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _svcmodule;
			}
			set
			{
				base.RepositoryItem = value;
				if ( value is ServiceModule ) {
					if ( value != _svcmodule && _svcmodule != null ) {
						try {
							_svcmodule.ItemChanged -= _svcmodule_ItemChanged;
						} catch { }
						value.ItemChanged += _svcmodule_ItemChanged;
					}
					_svcmodule = (ServiceModule)value;
					load();
				}
			}
		}

		void _svcmodule_ItemChanged(object sender, OpenComposite.EII.Services.RepositoryChangedEventArgs e)
		{
			if ( e.RepositoryItem is ServiceModule && e.Member != null && e.Member.Name == "Name" ) {
				if ( e.NewValue != null && e.NewValue != e.OldValue ) {
					txtName.Text = e.NewValue.ToString();
					txtName.Refresh();
				}
			}
		}

		public override string TypeText
		{
			get { return Resources.sServiceModule; }
		}

		public override string TypeDescription
		{
			get { return ""; }
		}
		#endregion

		#region Events
		#endregion

		#region Methods
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private ServiceModule _svcmodule = null;
		#endregion

		#region Methods
		private void load()
		{
			if ( _svcmodule == null )
				return;

			txtName.Text = _svcmodule.Name;
			txtDesc.Text = _svcmodule.Description;

			dgvCapabilities.AutoGenerateColumns = false;
			dgvCapabilities.DataSource = _svcmodule.GetVerbs();

			if ( tabControl1.SelectedTab == tabUsedBy ) {
				usedByHierarchyServiceModule.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			} else if ( tabControl1.SelectedTab == tabUses ) {
				usingHierarchyCapability.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			}
		}
		private void save()
		{
			if ( _svcmodule == null )
				_svcmodule = new ServiceModule();

			_svcmodule.Reload();

			_svcmodule.Name = txtName.Text;
			_svcmodule.Description = txtDesc.Text;
		}
		#endregion

		private void tabControl1_Selected(object sender, TabControlEventArgs e)
		{
			if ( e.TabPage == tabUsedBy ) {
				usedByHierarchyServiceModule.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			} else if ( e.TabPage == tabUses ) {
				usingHierarchyCapability.RepositoryItem = this.RepositoryItem as RepositoryItemBaseEx;
			}
		}

		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( e.KeyChar < (char)32 ) return;
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		#region Event Handlers
		#endregion

		#endregion

		protected override void NotifyPropertyChange(string property, object value)
		{
			if ( property == "Name" && value != null ) {
				txtName.Text = value.ToString();
			}
		}
	}
}
